package com.ruoyi.jinchuguanli.controller;

import com.ruoyi.cangku.mapper.ShangpinguanliMapper;
import com.ruoyi.gongjulei;
import com.ruoyi.jinchuguanli.domain.Xiaoshouchuku;
import com.ruoyi.jinchuguanli.domain.XiaoshouchukuReturn;
import com.ruoyi.jinchuguanli.domain.shangpinPrint;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRGraphics2DExporter;
import net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @description JasperUtils
 */
@Component
public class JasperUtils {

    private static String env;

    @Value("${ruoyi.profile}")
    public void setEnv(String env) {
        JasperUtils.env = env;
    }

    public static String ChuKuDanSet(Xiaoshouchuku xiaoshouChuku, ShangpinguanliMapper shangpinguanliMapper) throws IOException, JRException {
        String tempath = "static/ChuKuDan.jrxml";
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources(tempath);
        Resource resource = resources[0];

        InputStream inputStream = resource.getInputStream();
        JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
        //放入需要循环获取的部分
        XiaoshouchukuReturn xiaoshouchukuReturn = gongjulei.aReturnFormat(xiaoshouChuku, shangpinguanliMapper);
        List<shangpinPrint> shangpinPrintList = new ArrayList<>();
        int size4EWai = 0;
        for (int i = 0; i < xiaoshouchukuReturn.getShangpin().size(); i++) {
            shangpinPrint shangpinPrint = new shangpinPrint();
            BeanUtils.copyProperties(xiaoshouchukuReturn.getShangpin().get(i), shangpinPrint);
            shangpinPrint.setBeizhu("  ");
            if (i == 0) {
                shangpinPrint.setBeizhu(xiaoshouChuku.getBeizhu());
            }
            if (shangpinPrint.getShangpinguigekuan() != 0) {
                size4EWai++;
            }
            shangpinPrintList.add(shangpinPrint);
        }
        JRDataSource dataSource = new JRBeanCollectionDataSource(shangpinPrintList);
        //放入固定参数部分
        Map<String, Object> printinfo = new HashMap<>();

        printinfo.put("fahuodanhao", xiaoshouChuku.getChukudanhao());
        printinfo.put("xiangmumingcheng", xiaoshouChuku.getXiangmumingcheng());
        printinfo.put("yewuyuan", xiaoshouChuku.getYewuyuan());
        printinfo.put("fahuoren", xiaoshouChuku.getFahuoren());
        printinfo.put("xiadanriqi", new SimpleDateFormat("yyyy.MM.dd").format(xiaoshouChuku.getXiadanriqi()));
        printinfo.put("shouhuodizhi", xiaoshouChuku.getShouhuodizhi());
        printinfo.put("zongjine", xiaoshouChuku.getZongjine());
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, printinfo, dataSource);

        // 创建graphics输出器
        JRGraphics2DExporter exporter = new JRGraphics2DExporter();
        // 创建一个影像对象
        BufferedImage bufferedImage = new BufferedImage(jasperPrint.getPageWidth() * 4,
                (230 + shangpinPrintList.size() * 30 + size4EWai * 10) * 4, BufferedImage.TYPE_INT_RGB);
        // 取graphics
        Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
        // 设置相应参数信息
        exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, g);
        exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, 4F);
        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
        exporter.exportReport();
        g.dispose();// 释放资源信息
        // 这里的bufferedImage就是最终的影像图像信息,可以通过这个对象导入.
        ImageIO.write(bufferedImage, "JPEG", new File(env + "/upload/ChuKuDan/" + xiaoshouChuku.getId() + "_" + xiaoshouChuku.getChukudanhao() + ".jpg"));

        inputStream.close();

        return env + "/upload/ChuKuDan/" + xiaoshouChuku.getId() + "_" + xiaoshouChuku.getChukudanhao() + ".jpg";
    }
}
